package com.spzx.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.RequiresLogin;
import com.spzx.service.ICartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    ICartService cartService;

    @PutMapping("/updateCartPrice")
    R<Boolean> updateCartPrice(){
        int i = cartService.updateCartPrice();
        return R.ok();
    }

    @DeleteMapping("/removeCheckedCart")
    R<Boolean> removeCheckedCart(){
        int i = cartService.removeCheckedCart();
        return R.ok();
    }

    @Operation(summary = "添加购物车")
    @RequiresLogin
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                @Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return success();
    }

    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("cartList")
    public AjaxResult cartList() {
        return success(cartService.getCartList());
    }

    @Operation(summary = "删除购物车信息")
    @RequiresLogin
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId){
        cartService.deleteCart(skuId);
        return success();
    }

    /*@Operation(summary = "选中购物车信息")
    @RequiresLogin
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId){
        cartService.checkCart(skuId);
        return success();
    }*/

    @Operation(summary="更新选中状态")
    @RequiresLogin
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart( @PathVariable(value = "skuId") Long skuId,@PathVariable(value = "isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return success();
    }


    @Operation(summary="更新所有选中状态")
    @RequiresLogin
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart( @PathVariable(value = "isChecked") Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return success();
    }


    @Operation(summary="清空购物车")
    @RequiresLogin
    @GetMapping("clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }
}
